home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 3 / Amiga Tools 3.iso / grafik / raytracing / rayshade-4.0.6.3 / libray / libcommon / rotate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-09  |  3.0 KB  |  120 lines

  1. /*
  2.  * rotate.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Craig E. Kolb
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  * 
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  * rotate.c,v 4.1 1994/08/09 07:54:59 explorer Exp
  17.  *
  18.  * rotate.c,v
  19.  * Revision 4.1  1994/08/09  07:54:59  explorer
  20.  * Bump version to 4.1
  21.  *
  22.  * Revision 1.1.1.1  1994/08/08  04:52:01  explorer
  23.  * Initial import.  This is a prerelease of 4.0.6enh3, or 4.1 possibly.
  24.  *
  25.  * Revision 4.0.1.1  91/09/29  15:35:38  cek
  26.  * patch1: Added comments.
  27.  * 
  28.  * Revision 4.0  91/07/17  14:31:18  kolb
  29.  * Initial version.
  30.  * 
  31.  */
  32. #include "common.h"
  33. #include "rotate.h"
  34.  
  35. TransMethods *iRotateMethods;
  36. void RotationMatrix();
  37.  
  38. /*
  39.  * Create and return reference to Rotate structure.
  40.  */
  41. Rotate *
  42. RotateCreate()
  43. {
  44.     Rotate *res;
  45.  
  46.     res = (Rotate *)Malloc(sizeof(Rotate));
  47.     res->x = res->y = res->theta = 0.;
  48.     res->z = 1.;
  49.     return res;
  50. }
  51.  
  52. /*
  53.  * Return a pointer to collection of methods for the
  54.  * Rotate transformation.
  55.  */
  56. TransMethods *
  57. RotateMethods()
  58. {
  59.     if (iRotateMethods == (TransMethods *)NULL) {
  60.         iRotateMethods = (TransMethods *)Malloc(sizeof(TransMethods));
  61.         iRotateMethods->create = (TransCreateFunc *)RotateCreate;
  62.         iRotateMethods->propagate = RotatePropagate;
  63.     }
  64.     return iRotateMethods;    
  65. }
  66.  
  67. /*
  68.  * Given a Rotate structure and forward and inverse transformations,
  69.  * propagate the information in the Rotate structure to the
  70.  * transformations.
  71.  */
  72. void
  73. RotatePropagate(rotate, trans, itrans)
  74. Rotate *rotate;
  75. RSMatrix *trans, *itrans;
  76. {
  77.     Vector axis;
  78.  
  79.     RotationMatrix(rotate->x, rotate->y, rotate->z, deg2rad(rotate->theta), trans);
  80.     /*
  81.      * Build the inverse...
  82.      */
  83.     MatrixInvert(trans, itrans);
  84. }
  85.  
  86. /*
  87.  * Initialize a rotation matrix given an axis of rotation and an
  88.  * angle.  Right-handed rotation is applied.
  89.  */
  90. void
  91. RotationMatrix(x, y, z, theta, trans)
  92. Float x, y, z, theta;
  93. RSMatrix *trans;
  94. {
  95.     Float n1, n2, n3, sintheta, costheta;
  96.     Vector vector;
  97.  
  98.     MatrixInit(trans);
  99.     vector.x = x;
  100.     vector.y = y;
  101.     vector.z = z;
  102.  
  103.     if (VecNormalize(&vector) == 0.)
  104.         RLerror(RL_WARN, "Degenerate rotation axis.\n");
  105.  
  106.     sintheta = sin(theta);
  107.     costheta = cos(theta);
  108.  
  109.     n1 = vector.x; n2 = vector.y; n3 = vector.z;
  110.     trans->matrix[0][0] = (Float)(n1*n1 + (1. - n1*n1)*costheta);
  111.     trans->matrix[0][1] = (Float)(n1*n2*(1 - costheta) + n3*sintheta);
  112.     trans->matrix[0][2] = (Float)(n1*n3*(1 - costheta) - n2*sintheta);
  113.     trans->matrix[1][0] = (Float)(n1*n2*(1 - costheta) - n3*sintheta);
  114.     trans->matrix[1][1] = (Float)(n2*n2 + (1 - n2*n2)*costheta);
  115.     trans->matrix[1][2] = (Float)(n2*n3*(1 - costheta) + n1*sintheta);
  116.     trans->matrix[2][0] = (Float)(n1*n3*(1 - costheta) + n2*sintheta);
  117.     trans->matrix[2][1] = (Float)(n2*n3*(1 - costheta) - n1*sintheta);
  118.     trans->matrix[2][2] = (Float)(n3*n3 + (1 - n3*n3)*costheta);
  119. }
  120.